package com.four.order.service.impl;

import com.four.common.utils.R;
import com.four.order.dao.LogisticsDao;
import com.four.order.dao.ShopExpressDao;
import com.four.order.dao.ShoppingDao;
import com.four.order.entity.OrLogistics;
import com.four.order.entity.OrShopExpress;
import com.four.order.entity.OrShopping;
import com.four.order.feign.HospitalFeign;
import com.four.order.service.ShopExpressService;
import com.four.order.vo.ShopExpressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: hxw
 * @Date: 2022/1/17 20:45
 */
@Service
public class ShopExpressServiceImpl implements ShopExpressService {
    @Autowired
    private ShopExpressDao expressDao;
    @Autowired
    private ShoppingDao shoppingDao;
    @Autowired
    private LogisticsDao logisticsDao;

    /**
     * 发货
     * @param shopExpressVo
     * @return
     */
    @Override
    public R deliverGoods(ShopExpressVo shopExpressVo) {
        //查询订单
        OrShopping byId = shoppingDao.getById(shopExpressVo.getOrId());
        //创建快递
        OrShopExpress orShopExpress = new OrShopExpress();
        orShopExpress.setExpressId(UUID.randomUUID()+"");
        orShopExpress.setExpText(shopExpressVo.getExpText());
        orShopExpress.setExpUserId(byId.getUserId());
        orShopExpress.setExpUserName(byId.getUserName());
        orShopExpress.setExpOrId(byId.getOrId());
        orShopExpress.setExpCreateTime(byId.getOrCreateTimem());
        orShopExpress.setExpDistribution(shopExpressVo.getExpDistribution());
        orShopExpress.setExpDistributionPrice(shopExpressVo.getExpDistributionPrice());

        orShopExpress.setExpAddress(shopExpressVo.getExpAddress());
        orShopExpress.setExpCompany(shopExpressVo.getExpCompany());
        expressDao.addshopExpress(orShopExpress);
        //创建 物流
        OrLogistics orLogistics = new OrLogistics();
        orLogistics.setExpressId(orShopExpress.getExpressId());
        //物流信息
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format = sdf.format(date);
        orLogistics.setLogisName(format+"\n"+orShopExpress.getExpCompany()+"现在已经开始发货");
        orLogistics.setLogisTime(date);
        orLogistics.setLogisticsCar(shopExpressVo.getExpCompany());
        logisticsDao.saveLogistics(orLogistics);

        //修改订单状态
        byId.setOrStatus(2);
        shoppingDao.updateByStatus(byId);
        return R.ok();
    }
}
